home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / tcombo.exe / TCOMBOBX.CPP < prev    next >
C/C++ Source or Header  |  1992-11-18  |  6KB  |  177 lines

  1. /*************************************************************************/
  2. /*                                                                       */
  3. /* TCOMBOBX.CPP                                                          */
  4. /*                                                                       */
  5. /* Copyright (c) 1992, Vincent J. Dentice                                */
  6. /* All rights reserved                                                   */
  7. /*                                                                       */
  8. /* The TComboBox class is an extension to Borland International's Turbo  */
  9. /* Vision Applications Framework for DOS.  It provides a class that      */
  10. /* acts like a Combo Box in other graphical environments like Microsoft  */
  11. /* Windows and IBM OS/2.                                                 */
  12. /*                                                                       */
  13. /* It is designed to be with a TDialog class and a TCollection Class.    */
  14. /*                                                                       */
  15. /*                                                                       */
  16. /*   Date    Prg  Ver  Description                                       */
  17. /* --------  ---  ---  ------------------------------------------------- */
  18. /* 09/30/92  VJD  0.1  Initial module definition.                        */
  19. /* 11/16/92  VJD  0.2  Added streamability to the TComboBox classes.     */
  20. /*                     Rewrote header files to behave like original      */
  21. /*                     Turbo Vision header files.                        */
  22. /* 11/17/92  VJD  0.3  Added functions dataSize, getData and setData.    */
  23. /* 11/18/92  RG   0.3  Added code to select the correct item when the    */
  24. /*                     TComboWindow comes up.  These code modifications  */
  25. /*                     where supplied by Robert Gloeckner (100034,3033). */
  26. /*                                                                       */
  27. /*************************************************************************/
  28.  
  29.  
  30. #define Uses_TComboBox
  31. #define Uses_TComboWindow
  32. #define Uses_TKeys
  33. #define Uses_TStreamableClass
  34. #include "tcombobx.h"
  35. #include <string.h>
  36.  
  37. #define cpComboBox "\x16"
  38.  
  39. char* TComboBox::icon = "\x19";
  40.  
  41.  
  42. TComboBox::TComboBox(const TRect& bounds, TInputLine *aLink, TCollection *aList) :
  43.         TView(bounds)
  44. {
  45.    options |= ofPostProcess;
  46.    eventMask |= evBroadcast;
  47.    link = aLink;
  48.    list = aList;
  49. }
  50.  
  51.  
  52. void TComboBox::shutDown()
  53. {
  54.    link = 0;
  55.    TView::shutDown();
  56. }
  57.  
  58.  
  59. ushort TComboBox::dataSize()
  60. {
  61.    return sizeof(void *);
  62. }
  63.  
  64.  
  65. void TComboBox::draw()
  66. {
  67.    TDrawBuffer b;
  68.  
  69.    b.moveStr(0, icon, getColor(0x01));
  70.    writeLine(0, 0, size.x, size.y, b);
  71. }
  72.  
  73.  
  74. void TComboBox::getData(void *rec)
  75. {
  76. //   TCollection **p = (TCollection **)rec;
  77. //   *p = list;
  78.    *(TCollection **)rec = list;
  79. }
  80.  
  81.  
  82. TPalette& TComboBox::getPalette() const
  83. {
  84.    static TPalette palette(cpComboBox, sizeof(cpComboBox)-1);
  85.    return palette;
  86. }
  87.  
  88.  
  89. void TComboBox::handleEvent(TEvent& event)
  90. {
  91.    TComboWindow *ComboWindow;
  92.    TRect  r, p;
  93.    ushort c;
  94.  
  95.    TView::handleEvent(event);
  96.    if ((event.what == evMouseDown) ||
  97.        (event.what == evKeyDown && ctrlToArrow(event.keyDown.keyCode) == kbDown
  98.                 && (link->state & sfFocused) != 0))
  99.    {
  100.       if (strlen(link->data))               // If length of link->data > 0,
  101.      list->insert(newStr(link->data));  // add new data to list
  102.       link->select();                       // Make InputLine the active view
  103.       r = link->getBounds();                // Get bounds of the InputLine
  104.       r.b.x += 1;                           // Extend x bound by 1
  105.       r.a.y += 1;                           // Move bound down by 1
  106.       r.b.y += 7;                           // Extend y bound by 7
  107.       p = owner->getExtent();               // Get extent of the Dialog Box
  108.       r.intersect(p);                       // Get intersection of Dialog and r
  109.       r.b.y -= 1;
  110.       ComboWindow = new TComboWindow(r, list); // Create a new TComboWindow
  111.       if (ComboWindow != 0) {
  112.      if (strlen(link->data))
  113.         ComboWindow->setSelection(link->data);
  114.  
  115.      c = owner->execView(ComboWindow);           // Execute TComboWindow as modal view
  116.  
  117.      if (c == cmOK) {                            // If TComboWindow return cmOK
  118.         char rslt[256];
  119.  
  120.         ComboWindow->getSelection(rslt);         // Set the link data to the selection
  121.         strncpy(link->data, rslt, link->maxLen);
  122.         link->selectAll(True);                   // Select all in the linked view
  123.         link->drawView();                        // Redraw the linked view
  124.      }
  125.      destroy(ComboWindow);
  126.       }
  127.       clearEvent(event);
  128.    }
  129.    else if (event.what == evBroadcast)
  130.       if ((event.message.command == cmReleasedFocus && event.message.infoPtr == link)
  131.        || (event.message.command == cmRecordHistory))
  132.      if (strlen(link->data))               // If length of link->data > 0,
  133.         list->insert(newStr(link->data));  // add new data to list
  134. }
  135.  
  136.  
  137. void TComboBox::newList(TCollection *aList)
  138. {
  139.    if (list)
  140.       destroy(list);
  141.  
  142.    list = aList;
  143. }
  144.  
  145.  
  146. void TComboBox::setData(void *rec)
  147. {
  148.     TCollection *p = (TCollection *)rec;
  149.  
  150.     newList(p);
  151. }
  152.  
  153.  
  154. void TComboBox::write(opstream& os)
  155. {
  156.    TView::write(os);
  157.    os << link << list;
  158. }
  159.  
  160.  
  161. void *TComboBox::read(ipstream& is)
  162. {
  163.    TView::read(is);
  164.    is >> link >> list;
  165.  
  166.    return this;
  167. }
  168.  
  169.  
  170. TStreamable *TComboBox::build()
  171. {
  172.    return new TComboBox(streamableInit);
  173. }
  174.  
  175.  
  176. TComboBox::TComboBox(StreamableInit) : TView(streamableInit) { }
  177.